/*
 * Copyright 2005-2013 biz-united. All rights reserved.
 * Support: http://www.biz-united.com.cn
 * License: http://www.biz-united.com.cn/?page_id=5&act=3
 */
package net.biz.template.directive;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import net.biz.Page;
import net.biz.Pageable;
import net.biz.entity.Brand;
import net.biz.entity.Product;
import net.biz.entity.ProductCategory;
import net.biz.entity.Promotion;
import net.biz.service.ProductCategoryService;
import net.biz.service.ProductService;

import org.springframework.stereotype.Component;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import net.biz.Pageable;

/**
 * 模板指令 - 顶级商品分类列表
 * 
 * @author BIZ Team
 * @version 3.0
 */
@Component("productCategoryRootListDirective")
public class ProductCategoryRootListDirective extends BaseDirective {

	/** 变量名称 */
	private static final String VARIABLE_NAME = "productCategories";

	@Resource(name = "productCategoryServiceImpl")
	private ProductCategoryService productCategoryService;
	
	@Resource(name = "productServiceImpl")
	private ProductService productService;
	

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		List<ProductCategory> productCategories;
		boolean useCache = useCache(env, params);
		String cacheRegion = getCacheRegion(env, params);
		Integer count = getCount(params);
		if (useCache) {
			productCategories = productCategoryService.findRoots(count, cacheRegion);
		} else {
			productCategories = productCategoryService.findRoots(count);
		}
		
		for(ProductCategory root : productCategories) {    	
			//System.out.println(root.getName()+"--"+root.getPath()+"   "+root.getId());
			
			//ProductCategory productCategory = productCategoryService.find(root.getId());
			ProductCategory productCategory = root;
			Pageable pageable = new Pageable(null, null);
			
			Page<Product> page = productService.findPage(productCategory, null, null, null, null, null, null, true, true, null, false, null, null, null, pageable);
			List<Product> content = page.getContent();
			Set<Product> products = new HashSet<Product>();//获取顶级分类对应的产品
			
			
			//for(Brand brand : productCategory.getBrands()){
				//System.out.println("    品牌: "+brand.getName());
			//}
			
			for(Product pro : content){
				if(pro.getIsList()&&pro.getIsMarketable()&&!pro.getIsDelete()&&!pro.getIsGift()){
					products.add(pro);
					//System.out.println("    产品: "+pro.getName()+"--"+pro.getPath()+"--"+pro.getPrice()+"--"+pro.getImage());
				}
			}
			
			root.setBrands(productCategory.getBrands());
			root.setProducts(products);
	  	}
		setLocalVariable(VARIABLE_NAME, productCategories, env, body);
	}
}